package com.jingyanzi.marketing.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.VoucherMapper;
import com.jingyanzi.marketing.domain.VoucherDo;
import com.jingyanzi.marketing.entity.MaVoucher;
import com.jingyanzi.marketing.entity.MaVoucherExample;
import com.jingyanzi.marketing.entity.MaVoucherExample.Criteria;
import com.jingyanzi.marketing.query.VoucherQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IVoucherService;
import com.jingyanzi.marketing.util.BeanUtilsExtends;

@Service("maVoucherService")
public class VoucherServiceImpl extends BaseServiceImpl implements IVoucherService {

	@Autowired
	private VoucherMapper voucherMapper;

	public ResultDO add(VoucherDo maVoucherDo) {
		MaVoucher voucher = new MaVoucher();

		ResultSupport result = BeanUtilsExtends.copy(voucher, maVoucherDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		voucher.setCreatetime(Calendar.getInstance().getTime());
		voucher.setIsDelete(1);
		int r = -1;

		try {
			r = voucherMapper.insert(voucher);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加凭证信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加凭证信息失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, voucher.getId());
		return result;
	}

	public ResultDO modifi(VoucherDo maVoucherDo) {
		MaVoucher voucher = new MaVoucher();

		ResultSupport result = BeanUtilsExtends.copy(voucher, maVoucherDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			r = voucherMapper.updateByPrimaryKeySelective(voucher);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改凭证信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改凭证信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO remove(Integer id) {
		MaVoucher voucher = new MaVoucher();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		voucher.setId(id);
		voucher.setIsDelete(-1);
		int r = -1;

		try {
			r = voucherMapper.updateByPrimaryKeySelective(voucher);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除凭证信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除凭证信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO get(Integer id) {
		MaVoucher voucher = new MaVoucher();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		try {
			voucher = voucherMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取凭证信息失败");
			result.setSuccess(false);
			return result;
		}

		if (voucher == null) {
			result.setErrorCode(ResultDO.VOUCHER_GET_ERROR);
			result.setErrorMsg("获取凭证信息失败");
			result.setSuccess(false);
			return result;
		}
		VoucherDo voucherDo = getMaVoucherDo(voucher);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, voucherDo);
		return result;
	}

	public ResultDO query(VoucherQuery maVoucherQuery) {
		ResultSupport result = new ResultSupport();

		List<MaVoucher> list = null;

		MaVoucherExample example = new MaVoucherExample();

		Criteria createCriteria = example.createCriteria();

		if (StringUtils.isNotBlank(maVoucherQuery.getBusinessName())) {
			createCriteria.andBusinessNameEqualTo(maVoucherQuery.getBusinessName());
		}
		if (StringUtils.isNotBlank(maVoucherQuery.getVoucher())) {
			createCriteria.andVoucherEqualTo(maVoucherQuery.getVoucher());
		}
		if (maVoucherQuery.getType() != null) {
			createCriteria.andTypeEqualTo(maVoucherQuery.getType());
		}
		createCriteria.andIsDeleteEqualTo(1);

		List<VoucherDo> voucherDos = null;

		if (maVoucherQuery.isPage()) {
			example.setLimitByPage(maVoucherQuery.getStart() + "," + maVoucherQuery.getPageRows());
			long count = voucherMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = voucherMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取凭证信息失败");
			result.setSuccess(false);
			return result;
		}
		voucherDos = getMaVoucherList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, voucherDos);

		return result;
	}

}
